package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.domain.Product;
import com.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ProductController
 * @Author 波
 * @Date 2024/7/29 21:15
 */
@RestController
@RequestMapping("/product")
@Tag(name = "商品管理接口")
public class ProductController extends BaseController {

    @Resource
    private ProductService productService;

    @Operation(summary = "获取商品的分页信息")
    @GetMapping("/page")
    public TableDataInfo getProductPageList(Product product) {
        startPage();
        List<Product> productList = productService.getProductPageList(product);
        return getDataTable(productList);
    }

    @Operation(summary = "添加商品信息")
    @PostMapping
    public AjaxResult addProduct(@RequestBody Product product) {
        return toAjax(productService.addProduct(product));
    }

    @Operation(summary = "根据ID获取商品详情信息")
    @GetMapping("/{id}")
    public AjaxResult selectProductDetailInfoById(@PathVariable Long id) {
        return success(productService.selectProductDetailInfoById(id));
    }

    @Operation(summary = "修改商品信息")
    @PutMapping
    public AjaxResult updateProduct(@RequestBody Product product) {
        return toAjax(productService.updateProduct(product));
    }

    @Operation(summary = "根据ID删除商品信息")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteProductByIds(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }

    @Operation(summary = "根据ID设置商品审核信息")
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return success();
    }
    @Operation(summary = "更新上下架状态")
    @GetMapping("updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return success();
    }

    @InnerAuth
    @GetMapping("getProduct/{pid}")
    public R<Product> selectProductById(@PathVariable("pid") Long pid) {
        return R.ok(productService.getById(pid));
    }


}
